package com.ruoyi.manage.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.manage.service.IManageUserService;
import com.ruoyi.manage.util.PhoneFormatCheckUtils;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KE_XUE
 * @Date: 2019/4/22
 * @Description: 用户注册
 */
@Api("业主注册管理")
@RestController
@RequestMapping("/manage/user")
public class RegisterController extends BaseController {

    private String prefix = "manage/user";

    @Autowired
    private IManageUserService manageUserService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  @Author: KE_XUE
     *  @Date: 2019/4/23
     *  @Description: 发送短信
     */
    @ApiOperation("发送短信")
    @PostMapping("/sendCode")
    public AjaxResult sendCode(String phonenumber){
        if(!PhoneFormatCheckUtils.isPhoneLegal(phonenumber)){
            return error("手机格式不正确");
        }
        try {
            String smsCode = (String) redisTemplate.opsForValue().get(phonenumber);
            if(smsCode != null){
                return error("您的验证码还未过期，请稍后重试");
            } else {
                manageUserService.createSmsCode(phonenumber);
                return success("验证码发送成功");
            }
        } catch (Exception e) {
            return error("验证码发送失败");
        }
    }

//    /**
//     * 新增业主
//     */
//    @ApiOperation("新增业主")
//    @GetMapping("/add")
//    public String add(ModelMap mmap) {
//        List<SysRole> sysRoleList = new ArrayList<>();
//        SysRole sysRole = new SysRole();
//        // 5目前写死代表用户角色为业主
//        sysRole.setRoleId(5L);
//        sysRoleList.add(sysRole);
//        mmap.put("roles", sysRoleList);
//        return "system/user/add";
//    }

    /**
     * 新增保存业主
     */
    @ApiOperation("新增业主")
    @Log(title = "业主管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUser user)
    {
        //校验验证码是否正确
        boolean checkSmsCode = manageUserService.checkSmsCode(user.getPhonenumber(), user.getSmsCode());
        if(!checkSmsCode){
            return error("验证码不正确");
        }
        if (StringUtils.isNotNull(user.getUserId()) && SysUser.isAdmin(user.getUserId()))
        {
            // TODO 后面增加进入黑名单功能
            return error("你已进入黑名单");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        user.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(userService.insertUser(user));
    }
}
